package jastgen.generator;

import static jastgen.generator.Helper.*;
import static jastgen.generator.Generator.*;

import java.io.PrintWriter;
import java.util.*;

import jastgen.CompileException;
import jastgen.nodes.ClassDef;
import jastgen.nodes.Field;
import jastgen.nodes.Module;
import jastgen.nodes.Type;

class IsBodyGenerator implements BodyGenerator {

	private final Module node;

	IsBodyGenerator(
			final Module node
		) throws CompileException {

		this.node = node;
	}

	public void generate(
			final PrintWriter out)
	{
		generateVisitorBody(out, node);
	}

	private void generateVisitorBody(
			final PrintWriter out,
			final Module node)
	{
		out.println("/**");
		out.println(" * ");
		out.println(" */");

		out.println("	public final class Is {");
		out.println();
		out.println("	private Is() {");
		out.println("	}");
		out.println();

		// クラスごとのメソッドを出力
		for(final ClassDef i : node.getClassDefs())
			printClassDef(out, i);

		out.println("}");
	}

	private void printClassDef(
			final PrintWriter out,
			final ClassDef i)
	{
		// インターフェースを持たないクラスは判定不要
		if(!i.hasInterface())
			return;

		final String bn = i.getBaseName();
		final String cn = i.getNameQs();
		final String in = i.getNameQsIF();

		out.println();
		out.println("	/**");
		out.println("	 * n instanceof " + cn);
		out.println("	 */");
		out.println("	public static boolean is" + bn + "(");
		out.println("			final " + in + " n)");
		out.println("	{");
		out.println("		return n instanceof " + cn + ";");
		out.println("	}");
	}
}
